﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace UdrinkWater_Project.Admin
{
    public partial class OrderMaterialDetail : System.Web.UI.Page
    {
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        DateTime OrderM_ID = DateTime.Now;
        DataSet ds = null;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ASPxGridView2_BeforePerformDataSelect(object sender, EventArgs e)
        {
            Session["OrderM_Date"] = (sender as ASPxGridView).GetMasterRowKeyValue();
        }

        protected void ASPxGridView1_RowCommand(object sender, ASPxGridViewRowCommandEventArgs e)
        {
            DataTable DataPro = new DataTable();
            if (e.CommandArgs.CommandName == "Cmd_Select")
            {
                string Id = e.CommandArgs.CommandArgument.ToString();
                DataPro = GetTotal(Convert.ToDateTime(Id));
                hdvalue.Value = DataPro.Rows[0]["total"].ToString();
            }
        }

        private DataTable GetTotal(DateTime OrderM_Date)
        {
            string sOrderM_Date = OrderM_Date.ToString("yyyy-MM-dd HH:mm:ss", new System.Globalization.CultureInfo("en-US"));
            Session["UP"] = sOrderM_Date;
            string sqltext = @"select SUM(total) As total
                               from
                               (SELECT [Material_Count] *[Price] As total
                                FROM TB_OrderMaterial_list
                                where [OrderM_Date] = '" + sOrderM_Date + "') As total";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];

        }      

        protected void ASPxButton2_Click(object sender, EventArgs e)
        {
            string xxx = Session["UP"].ToString();
            string sqltext1 = @" update TB_Order_Material set M_Status_ID = @Mid 
                                 where OrderM_Date = '" + Session["UP"].ToString() + "'";
            SqlCommand sqlcomm1 = new SqlCommand();
            SqlConnection sqlconn1 = new SqlConnection(Connection);
            sqlcomm1.Connection = sqlconn1;
            sqlcomm1.CommandType = System.Data.CommandType.Text;
            sqlcomm1.CommandText = sqltext1;
            sqlcomm1.Parameters.AddWithValue("@Mid", DropDownList1.SelectedValue);
            try
            {
                sqlcomm1.Connection.Open();
                sqlcomm1.ExecuteNonQuery();
            }
            catch (Exception)
            {
                sqlcomm1.Connection.Close();
                sqlcomm1.Connection.Dispose();
            }
            sqlcomm1.Connection.Close();
            sqlcomm1.Connection.Dispose();

            sqltext1 = @"insert into TB_Expenditure ([Expenditure_Date]
                                                    ,[Expenditure_List]
                                                    ,[Expenditure_Price]
                                                    ,[Referent])
                        VALUES  (@dateNow,@name,@money,@dateM)";
            SqlConnection sqlconn2 = new SqlConnection(Connection);
            sqlcomm1.Connection = sqlconn2;
            sqlcomm1.CommandType = System.Data.CommandType.Text;
            sqlcomm1.CommandText = sqltext1;
            sqlcomm1.Parameters.AddWithValue("@dateNow", DateTime.Now);
            sqlcomm1.Parameters.AddWithValue("@name", "จัดซื้อวัตถุดิบ");
            sqlcomm1.Parameters.AddWithValue("@money", hdvalue.Value);
            sqlcomm1.Parameters.AddWithValue("@dateM", xxx);
            try
            {
                sqlcomm1.Connection.Open();
                sqlcomm1.ExecuteNonQuery();
            }
            catch (Exception)
            {
                sqlcomm1.Connection.Close();
                sqlcomm1.Connection.Dispose();
            }

            sqlcomm1.Connection.Close();
            sqlcomm1.Connection.Dispose();
            Response.Redirect("~/Admin/OrderMaterialDetail.aspx");
        }
    }
}